Spot-On
spot-on-utilities.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_utilities_h_
29 #define _spoton_utilities_h_
30 
31 #include <QDesktopWidget>
32 #include <QScreen>
33 #include <QWidget>
34 
36 {
37  public:
38  static void centerWidget(QWidget *child, QWidget *parent)
39  {
40  if(!child || !parent)
41  return;
42 
43  /*
44  ** From QDialog.
45  */
46 
47 #if QT_VERSION >= 0x050000
48 #ifdef Q_WS_X11
49  if(X11->isSupportedByWM(ATOM(_NET_WM_FULL_PLACEMENT)))
50  return;
51 #endif
52 #endif
53 
54 #ifdef Q_OS_SYMBIAN
55  /*
56  ** Perhaps implement symbianAdjustedPosition().
57  */
58 #endif
59 
60  QPoint p(0, 0);
61  int extraw = 0, extrah = 0, scrn = 0;
62 
63  if(parent)
64  parent = parent->window();
65 
66  QRect desk;
67 
68  if(parent)
69  scrn = QApplication::desktop()->screenNumber(parent);
70 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
71  else if(QApplication::desktop()->isVirtualDesktop())
72  scrn = QApplication::desktop()->screenNumber(QCursor::pos());
73 #endif
74  else
75  scrn = QApplication::desktop()->screenNumber(child);
76 
77 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
78  desk = QApplication::desktop()->availableGeometry(scrn);
79 #else
80  desk = QGuiApplication::screens().value(scrn) ?
81  QGuiApplication::screens().value(scrn)->geometry() : QRect();
82 #endif
83 
84  QWidgetList list = QApplication::topLevelWidgets();
85 
86  for(int i = 0; (extraw == 0 || extrah == 0) && i < list.size(); ++i)
87  {
88  QWidget *current = list.at(i);
89 
90  if(current->isVisible())
91  {
92  int frameh = current->geometry().y() - current->y();
93  int framew = current->geometry().x() - current->x();
94 
95  extraw = qMax(extraw, framew);
96  extrah = qMax(extrah, frameh);
97  }
98  }
99 
100  if(extraw == 0 || extrah == 0 || extraw >= 10 || extrah >= 40)
101  {
102  extrah = 40;
103  extraw = 10;
104  }
105 
106  if(parent)
107  {
108  QPoint pp = parent->mapToGlobal(QPoint(0,0));
109 
110  p = QPoint(pp.x() + parent->width() / 2,
111  pp.y() + parent->height() / 2);
112  }
113  else
114  p = QPoint(desk.x() + desk.width() / 2, desk.y() + desk.height() / 2);
115 
116  p = QPoint(p.x() - child->width() / 2 - extraw,
117  p.y() - child->height() / 2 - extrah);
118 
119  if(p.x() + extraw + child->width() > desk.x() + desk.width())
120  p.setX(desk.x() + desk.width() - child->width() - extraw);
121 
122  if(p.x() < desk.x())
123  p.setX(desk.x());
124 
125  if(p.y() + extrah + child->height() > desk.y() + desk.height())
126  p.setY(desk.y() + desk.height() - child->height() - extrah);
127 
128  if(p.y() < desk.y())
129  p.setY(desk.y());
130 
131  child->move(p);
132  }
133 
134  private:
135  spoton_utilities(void);
136 };
137 
138 #endif
Definition: spot-on-utilities.h:35